<?php

class AdminStoreController extends AdminController
{
  use ApiTrait;
  public static function __permissions()
  {
    return [
      'name' => '店铺管理',
      'permissions' => [
        'store.itemcate.list' => '查看店铺商品分类列表',
        'store.itemcate.edit' => '新增／编辑店铺商品分类',
        'store.item.list' => '查看店铺商品列表',
        'store.item.edit' => '新增管理／编辑店铺商品',
      ],
    ];
  }
  public function cateAction()
  {
    if (!$this->account->checkPermission(['store.itemcate.list', 'store.item.list', 'store.item.edit'], 'or')) {
      $this->addErrorMessage('您未拥有相应权限！！');
      $this->gotoUrl('admin', true);
    }
    $storeModel = $this->getModel('store');
    if ($this->req->isPost()) {
      if (!$this->account->checkPermission(['store.itemcate.edit'])) {
        $this->addErrorMessage('您未拥有相应权限！！');
        $this->gotoUrl('admin', true);
      }
      $name = $this->req->getPostTrim('name');
      if (hasEmpty([$name])) {
        $this->addErrorMessage('参数错误！！');
      } else if ($storeModel->createItemCate([
        'name' => $name, 'user_id' => $this->account->user_id
      ])) {
        $this->addSuccessMessage('添加商品分类[' . $name . ']成功！！');
      } else {
       $this->addErrorMessage('添加商品分类[' . $name . ']失败，请重新添加！！');
      }
      $this->gotoUrl('admin/store/cate', true);
    }
    $page = max(intval($this->req->getQuery('page', 1)), 1);
    $rows = 15;
    $query = [
      'search' => trim($this->req->getQuery('search', '')),
      'status' => trim($this->req->getQuery('status', '')),
    ];
    $view = $this->getView();
    $list = $storeModel->getItemCatesList($query, $page, $rows);
    $count = $storeModel->getItemCatesCount($query);
    if ($this->req->isAjax()) {
      return $this->okReturn([
        'list' => $list,
        'next' => count($list) == $rows ,
      ]);
    }
    $view->assign([
      'query' => $query,
      'list' => $list, 'count' => $count,
      'page' => $page, 'rows' => $rows
    ]);
    $view->display('admin/store/cate.phtml');
  }

  public function itemCateUpdatedAction($op, $cateid)
  {
    if (!$this->account->checkPermission(['store.itemcate.edit'])) {
      $this->addErrorMessage('您未拥有相应权限！！');
      $this->gotoUrl('admin', true);
    }
    $storeModel = $this->getModel('store');
    if (!($itemCate = $storeModel->getItemCate($cateid))) {
      $this->addErrorMessage('参数错误！！');
      $this->gotoUrl('admin/store/cate', true);
    } else if ($op == 'delete') {
      if ($itemCate->delete()) {
        $this->addSuccessMessage('删除商品类目[' . $itemCate->name .  '] 成功');
      } else {
        $this->addErrorMessage('删除商品类目[' . $itemCate->name .  '] 失败,请检查该类目下是否还有商品');
      }
    } else if ($op == 'online') {
      $itemCate->status = ItemCate::IS_ONLINE;
      if ($itemCate->update()) {
        $this->addSuccessMessage('商品类目[' . $itemCate->name.  '] 上架成功');
      } else {
        $this->addErrorMessage('商品类目[' . $itemCate->name.  '] 上架失败！！');
      }
    } else if ($op == 'offline') {
      $itemCate->status = ItemCate::IS_OFFLINE;
      if ($itemCate->update()) {
        $this->addSuccessMessage('商品类目[' . $itemCate->name.  '] 下架成功');
      } else {
        $this->addErrorMessage('商品类目[' . $itemCate->name.  '] 下架失败！！');
      }
    }
    $this->gotoUrl('admin/store/cate', true);
  }

  public function itemAction()
  {
    if (!$this->account->checkPermission(['store.item.list'])) {
      $this->addErrorMessage('您未拥有相应权限！！');
      $this->gotoUrl('admin', true);
    }
    $storeModel = $this->getModel('store');
    $page = max(intval($this->req->getQuery('page', 1)), 1);
    $rows = 15;
    $itemCate = $storeModel->getItemCate($this->req->getQuery('cate_id', ''));
    $query = [
      'search' => trim($this->req->getQuery('search', '')),
      'status' => trim($this->req->getQuery('status', '')),
      'cate_id' => $itemCate ? $itemCate->cate_id: null,
    ];
    $view = $this->getView();
    $list = $storeModel->getItemsList($query, $page, $rows);
    $count = $storeModel->getItemsCount($query);
    $view->assign([
      'query' => $query,
      'list' => $list, 'count' => $count,
      'page' => $page, 'rows' => $rows,
      'itemCate' => $itemCate
    ]);
    $view->display('admin/store/item.phtml');
  }

  public function itemAddAction()
  {
    if (!$this->account->checkPermission(['store.item.edit'])) {
      $this->addErrorMessage('您未拥有相应权限！！');
      $this->gotoUrl('admin', true);
    }
    $view = $this->getView();
    if ( $this->req->isPost()) {
      $storeModel = $this->getModel('store');
      $image = $this->req->getPostTrim('image');
      $name = $this->req->getPostTrim('name');
      $describe = $this->req->getPostTrim('describe');
      $lifetime = intval('0' . $this->req->getPostTrim('lifetime')) ;
      $flowers = intval('0' . $this->req->getPostTrim('flowers')) ;
      $cate_id = $this->req->getPostTrim('cate_id');
      $status = $this->req->getPostTrim('status') == Item::IS_ONLINE ? Item::IS_ONLINE: Item::IS_OFFLINE;
      if (hasEmpty([$image, $name, $describe, $lifetime, $flowers ]) || !(($itemCate = $storeModel->getItemCate($cate_id)))) {
        $this->addErrorMessage('参数错误！！');
        $this->res->reload();
      }
      $this->getSession()->set('addItem', [ 'data' => $this->req->getPost(), 'cate' => [ 'cate_id' => $itemCate->cate_id, 'catename' => $itemCate->name]]);
      if ($storeModel->createItem([
        'user_id' => $this->account->user_id,
        'cate_id' => $itemCate->cate_id,
        'image' => $image, 'name' => $name, 'status' => $status,
        'describe' => $describe, 'lifetime' => $lifetime, 'flowers' => $flowers
      ])) {
        $this->addSuccessMessage('添加商品[ ' . $name . ' ]成功');
        $this->getSession()->set('addItem', null);
        $this->res->gotoUrl('admin/store/item');
      } else {
        $this->addErrorMessage('添加商品[ ' . $name . ' ]失败，请重试');
        $this->res->reload();
      }
    }
    $view->display('admin/store/item_add.phtml');
  }

  public function itemUpdatedAction($op, $itemid)
  {
    if (!$this->account->checkPermission(['store.item.edit'])) {
      $this->addErrorMessage('您未拥有相应权限！！');
      $this->gotoUrl('admin', true);
    }
    $storeModel = $this->getModel('store');
    if (!($item = $storeModel->getItem($itemid))) {
      $this->addErrorMessage('参数错误！！');
      $this->gotoUrl('admin/store/item', true);
    } else if ($op == 'delete') {
      if ($item->delete()) {
        $this->addSuccessMessage('删除商品[' . $item->name .  '] 成功');
      } else {
        $this->addErrorMessage('删除商品[' . $item->name .  '] 失败,请检查该类目下是否还有商品');
      }
    } else if ($op == 'online') {
      $item->status = Item::IS_ONLINE;
      if ($item->update()) {
        $this->addSuccessMessage('商品[' . $item->name.  '] 上架成功');
      } else {
        $this->addErrorMessage('商品[' . $item->name.  '] 上架失败！！');
      }
    } else if ($op == 'offline') {
      $item->status = Item::IS_OFFLINE;
      if ($item->update()) {
        $this->addSuccessMessage('商品[' . $item->name.  '] 下架成功');
      } else {
        $this->addErrorMessage('商品[' . $item->name.  '] 下架失败！！');
      }
    }
    $this->gotoUrl('admin/store/item', true);
  }

}
